//************************************************************
//************************************************************
//  TickUtils
//  Copyright (c) 2009 by Charith Amarasinghe
//  SOME RIGHTS RESERVED
//************************************************************
//************************************************************
// Date: 9/03/2009                Coded by: Charith Amarasinghe
// Filename: TickUtils.pas        Module name: TickUtils
//                                Source file: TickUtils.pas 
// Program description:
// Utility to assist in drawing Ticks in TPendulumView
//
//************************************************************
// Libraries and software support:
// - Delphi 5 and higher
// - Required by PendulumView.pas
// - Requires Windows 2000 or higher (possible GDI limitations)
//
//************************************************************
// Development environment:
// - Borland Delphi 5
// - Windows 2003 Server SP3
//
//************************************************************
// System requirements:
// - Boarland Delphi 5 Compiler
// - Microsoft Windows
//************************************************************
// Start date: 2/03/2009
// Update history:
//            DATE            MODIFICATION
//			
//************************************************************
// Test history:
//  TEST PROTOCOL           DATE       TEST RESULTS
//	Testing in GUI			Rolling
//
//************************************************************
// Programmer comments:
//
//
//************************************************************

unit TickUtils;

interface
  uses Math, Graphics, Windows;
  
  
  const
  TICKLENGTH : integer = 13; //
  TICKDIST : integer = 100; //Depreciated

  function GetTickStartPoint(Origin: TPoint; Angle : real; DistanceFromOrigin: integer) : TPoint;
  function GetTickEndPoint(Origin:TPoint; Angle: real; TickLength: integer; DistanceFromOrigin: integer): TPoint;
  function GetAngleFromOrigin(Origin: TPoint; x,y: integer) : real;
  function GetAngleFromOriginV(Origin: TPoint; x,y: integer) : real;
  
implementation

  //function GetTickStartPoint
  //--------------------------
  //Origin: center of ticks
  //Angle: Angle from horizontal
  //DistanceFromOrigin: How far the ticks are from the Origin point
  //
  //Returns start point for tick line
  //--------------------------
  function GetTickStartPoint(Origin: TPoint; Angle : real; DistanceFromOrigin: integer) : TPoint;
  var
   x, y: integer;
  begin
    x:= round(DistanceFromOrigin*cos(angle))+Origin.X;
    y:= round(DistanceFromOrigin*sin(angle))+Origin.Y;

    GetTickStartPoint.x := x;
    GetTickStartPoint.y := y;
  end;

  //function GetTickEndPoint
  //--------------------------
  //Origin Point: origin point of ticks
  //Angle: Angle from horizontal
  //TickLength: How long the ticks are in pixels
  //DistanceFromOrigin:
  //
  //Returns end point for tick line
  //--------------------------
  function GetTickEndPoint(Origin:TPoint; Angle: real; TickLength: integer; DistanceFromOrigin: integer): TPoint;
  var
   x, y: integer;
  begin
    x:= round((TickLength+DistanceFromOrigin)*cos(angle))+Origin.X;
    y:= round((TickLength+DistanceFromOrigin)*sin(angle))+Origin.Y;

    GetTickEndPoint.x := x;
    GetTickEndPoint.y := y;
  end;
  
  //function GetAngleFromOrigin
  //--------------------------
  //Origin: center of angle measurement
  //x,y: position in canvas
  //
  //Returns angle made by point (x,y) at the origin from the horizontal.
  function GetAngleFromOrigin(Origin: TPoint; x,y: integer) : real;
  begin
    if (x-origin.x = 0) then
    begin
       GetAngleFromOrigin := pi;
    end else
    GetAngleFromOrigin := arctan2((y-origin.y),(x-origin.x));
  end;
  
  //function GetAngleFromOriginV
  //--------------------------
  //Origin: center of angle measurement
  //x,y: position in canvas
  //
  //Returns angle made by point (x,y) at the origin from the vertical.
  function GetAngleFromOriginV(Origin: TPoint; x,y: integer) : real;
  begin
    if (x-origin.x = 0) then
    begin
       GetAngleFromOriginV := pi;
    end else
    GetAngleFromOriginV := arctan2((x-origin.x),(y-origin.y));
  end;
end.


